package com.doubao.hardware.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@Slf4j
public class MqttAuthDebugUtil {

    public static void debugMqttAuth(
            String iotCoreId,
            String deviceKey,
            String deviceSecret,
            String algorithmType
    ) {
        // 获取当前时间戳
        Integer timestamp = CommonUtil.getSecondTimestampTwo(new Date());

        // 生成用户名
        String username = MqttAuthUtil.generateUsername(
                iotCoreId,
                deviceKey,
                timestamp,
                algorithmType
        );

        // 生成密码
        String password = MqttAuthUtil.generatePassword(
                deviceKey,
                timestamp,
                algorithmType,
                deviceSecret
        );

        // 详细日志输出
        log.info("MQTT Authentication Debug");
        log.info("IoT Core ID: {}", iotCoreId);
        log.info("Device Key: {}", deviceKey);
        log.info("Device Secret: {}", deviceSecret);
        log.info("Timestamp: {}", timestamp);
        log.info("Algorithm Type: {}", algorithmType);
        log.info("Generated Username: {}", username);
        log.info("Generated Password: {}", password);

        // 额外验证
        String signContent = deviceKey + "&" + timestamp + "&" + algorithmType + deviceSecret;
        log.info("Sign Content: {}", signContent);
        log.info("MD5 Sign Content: {}", DigestUtils.md5Hex(signContent));
    }

    public static void main(String[] args) {
        debugMqttAuth(
                "annpbks",       // IoT Core ID
                "Albot_Config",  // Device Key
                "OfwfUKDlsglPMGgx", // Device Secret
                "MD5"            // Algorithm Type
        );
    }
}